package com.qx.grainmall.member.service.impl;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.qx.common.utils.HttpUtils;
import com.qx.common.utils.PageUtils;
import com.qx.common.utils.Query;
import com.qx.grainmall.member.vo.WeiboAccessTokenVo;
import org.apache.http.HttpResponse;
import org.apache.http.util.EntityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.HashMap;
import java.util.Map;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

import com.qx.grainmall.member.dao.MemberDao;
import com.qx.grainmall.member.entity.MemberEntity;
import com.qx.grainmall.member.service.MemberService;


@Service("memberService")
public class MemberServiceImpl extends ServiceImpl<MemberDao, MemberEntity> implements MemberService {

    @Autowired
    private MemberDao memberDao;
    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        IPage<MemberEntity> page = this.page(
                new Query<MemberEntity>().getPage(params),
                new QueryWrapper<MemberEntity>()
        );

        return new PageUtils(page);
    }

    @Override
    public MemberEntity weiboLogin(WeiboAccessTokenVo vo) throws Exception {
        //根据access_token 判断该微博号是否注册
        MemberEntity memberEntity = memberDao.selectOne(new QueryWrapper<MemberEntity>().eq("social_uid", vo.getUid()));
        if(memberEntity != null){
            //以前登陆过 并且 微博也注册过了
            //更新过期时间
            memberEntity.setExpiresIn(vo.getExpires_in());
            memberEntity.setAccessToken(vo.getAccess_token());
            memberDao.updateById(memberEntity);
            return memberEntity;
        }else {
            //系统用户第一次登陆
            //要完成注册
            MemberEntity member = new MemberEntity();
            //远程调用微博查 用户信息接口
            HashMap<String, String> map = new HashMap<>();
            map.put("access_token",vo.getAccess_token());
            map.put("uid",vo.getUid());

            //执行查询
            HttpResponse httpResponse = HttpUtils.doGet("https://api.weibo.com/2/users/show.json", "", "get", null, map);
            String resp = EntityUtils.toString(httpResponse.getEntity(), "UTF-8");
            JSONObject jsonObject = JSON.parseObject(resp);
            member.setNickname((String) jsonObject.get("name"));
            member.setUsername((String) jsonObject.get("name"));
            //拼接剩下的信息
            int g=0;
            if(jsonObject.get("gender") != null){
                g = ((String) jsonObject.get("gender")).equals("m") ? 1 : 0;
            }
            member.setGender(g);
            member.setCreateTime(new Date());
            member.setSocialUid(vo.getUid());
            member.setExpiresIn(vo.getExpires_in());
            member.setAccessToken(vo.getAccess_token());
            //级别
            member.setLevelId(1L);
            memberDao.insert(member);

            return member;//自动主键返回
        }

    }

}